//
//  GoodsCatalogModel.m
//  RainbowFM
//
//  Created by Kratos on 14/12/19.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "GoodsCatalogModel.h"

#import "LoginModel.h"
#import "GoodsAttributeItem.h"

@implementation GoodsCatalogModel

+(instancetype)sharedModel
{
    static GoodsCatalogModel *goodsCatalogModel = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        goodsCatalogModel = [[GoodsCatalogModel alloc] init];
    });
    
    return goodsCatalogModel;
    
}

-(void)fetchGoodsFirstCatalogList:(void (^)(NSArray *, NSString *))res{
    StoreAPI *api = [[StoreAPI alloc] init];
    [api fetchGoodsFirstCatalogWithuserId:nil userCode:nil
                                   result:^(NSArray *catalog, NSString *msg) {
                                       NSMutableArray *listArray = [NSMutableArray array];
                                       for (NSDictionary *dict in catalog) {
                                           GoodsFirstCatalogItem *item = [[GoodsFirstCatalogItem alloc] initWithDataDic:dict];
                                           [listArray addObject:item];
                                       }
                                       
                                       res(listArray,@"");
                                   }];
    
}

-(void)fetchgoodsSecondCatalogListWithCatalogID:(NSString *)catalogId result:(void (^)(NSArray *, NSString *))res{
    StoreAPI *api = [[StoreAPI alloc] init];
    [api fetchGoodsSecondCatalogWithuserId:nil userCode:nil firstCatalogId:catalogId
                                    result:^(NSArray *catalog, NSString *msg) {
                                        NSMutableArray *listArray = [NSMutableArray array];
                                        for (NSDictionary *dict in catalog) {
                                            GoodsSecondCatalogItem *item = [[GoodsSecondCatalogItem alloc] initWithDataDic:dict];
                                            [listArray addObject:item];
                                        }
                                        
                                        res(listArray,@"");
                                    }];


}

-(void)fetchGoodsListWithFirstCatalogID:(NSString *)firstId secondCatalogID:(NSString *)secondId key:(NSString *)key_ sortFlag:(int)sortFlag_ currentPage:(int)start pageSize:(int)count result:(void (^)(NSArray *, NSString *))res{
    StoreAPI *api = [[StoreAPI alloc] init];
    [api searchGoodsWithuserId:nil
                      userCode:nil
                firstCatalogId:firstId
               secondCatalogId:secondId
                           key:key_
                      sortFlag:[NSString stringWithFormat:@"%d",sortFlag_]
                   currentPage:[NSString stringWithFormat:@"%d",start]
                      pageSize:[NSString stringWithFormat:@"%d",count]
                        result:^(NSArray *goodsList, NSString *msg) {
                            NSMutableArray *listArray = [NSMutableArray array];
                            for (NSDictionary *dict in goodsList) {
                                GoodsInfoItem *item = [[GoodsInfoItem alloc] initWithDataDic:dict];
                                [listArray addObject:item];
                            }
                            
                            res(listArray,@"");
                        }];
}

-(void)fetchGoodsDetailWithProductId:(NSString *)productId
                              result:(void(^)(GoodsAttributeItem *item, NSString *msg))res{
    StoreAPI *api = [[StoreAPI alloc] init];
    [api fetchGoodsDetailWithuserId:nil userCode:nil productId:productId result:^(NSArray *goodsDetail, NSString *msg) {
        if (goodsDetail) {
            NSDictionary *dic = [goodsDetail firstObject];
            GoodsAttributeItem *item = [[GoodsAttributeItem alloc]initWithDataDic:dic];
            
            res(item,msg);
        }else{
            res(nil,msg);
        }

    }];
}

@end

@implementation GoodsInfoItem

- (NSDictionary *)attributeMapDictionary
{
    return @{
             @"FIRST_CATALOG_ID":@"FIRST_CATALOG_ID",
             @"PRE_PRICE":@"PRE_PRICE",
             @"PRICE":@"PRICE",
             @"PRODUCT_ID":@"PRODUCT_ID",
             @"PRODUCT_IMG_URL":@"PRODUCT_IMG_URL",
             @"PRODUCT_NAME":@"PRODUCT_NAME",
             @"SALE_NUM":@"SALE_NUM",
             @"SECOND_CATALOG_ID":@"SECOND_CATALOG_ID"
             };
}

//-(NSString *)PRICE
//{
//    if (_PRICE && _PRICE.length > 0) {
//        CGFloat price = [_PRICE floatValue]/100;
//        return  _F(@"¥%.1f",price);
//    }
//    
//    return _PRICE;
//}
//
//-(NSString *)PRE_PRICE
//{
//    if (_PRE_PRICE && _PRE_PRICE.length > 0) {
//        CGFloat price = [_PRE_PRICE floatValue]/100;
//        return _F(@"¥%.1f",price);
//    }
//    
//    return _PRE_PRICE;
//}

@end


@implementation GoodsCatalogItem

@end

@implementation GoodsFirstCatalogItem

- (NSDictionary *)attributeMapDictionary
{
    return @{
             @"CATALOG_ID":@"FIRST_CATALOG_ID",
             @"CATALOG_NAME":@"FIRST_CATALOG_NAME"
             };
}




@end

@implementation GoodsSecondCatalogItem

- (NSDictionary *)attributeMapDictionary
{
    return @{
             @"CATALOG_ID":@"SECOND_CATALOG_ID",
             @"CATALOG_NAME":@"SECOND_CATALOG_NAME"
             };
}


@end